const path = require('path')
const webpack = require('webpack')
const CompressionPlugin = require('compression-webpack-plugin')
const system = require('./src/config/system')
const modifyVars = require('./src/config/theme')
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
function resolve (dir) {
  return path.join(__dirname, dir)
}
const isDev = process.env.NODE_ENV === 'development'

/*const APIList = {
  newManageDev: 'http://kong.poros-platform.10.74.158.69.nip.io/', // 最新平台管理开发环境
  oldTechDev: 'http://kong.poros-platform.10.74.166.10.nip.io/' // 旧中台开发环境
}
const proxyAPI = APIList.newManageDev*/

module.exports = {
  lintOnSave: false,
  productionSourceMap: false,
  publicPath: system.baseUrl,
  configureWebpack: {
    // 避免 chrome 调试时 断点 不准确
    devtool: isDev ? 'source-map' : undefined,

    plugins: [
      new HardSourceWebpackPlugin(),
      new webpack.DllReferencePlugin({
        context: process.cwd(),
        manifest: require('./public/vendor/vendor-manifest.json')
      })
    ]
  },
  chainWebpack (config) {
    // html-webpack-plugin 配置
    config.plugin('html').tap(args => {
      Object.assign(args[0], {
        favicon: './public/favicon.ico',
        dll: path
          .join(isDev ? '' : system.baseUrl, '/vendor/vendor.dll.js')
          .replace(/\\/g, '/')
      })

      return args
    })

    config.plugins.delete('prefetch')
    // ie兼容
    config.entry('main').add('babel-polyfill')
    // 处理svg图片
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/svg'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    if (!isDev) {
      // 开启gzip
      config.plugin('compressionPlugin')
        .use(new CompressionPlugin({
          test: /\.js$|\.html$|.\css/, // 匹配文件名
          threshold: 10240, // 对超过10k的数据压缩
          deleteOriginalAssets: false // 不删除源文件
        }))
        .end()
    }
  },
  css: {
    loaderOptions: {
      less: {
        modifyVars,
        // DO NOT REMOVE THIS LINE
        javascriptEnabled: true
      }
    }
  },
  devServer: {
    proxy: {
      '/api': {
        target: system.baseApi,
        // target: 'http://10.110.96.153:8030/',
        changeOrigin: true
      },
      // 图片
      '/minio/poros': {
        target: system.baseApi
      }
    }
  }
}
